﻿using System.Windows;
using System.Windows.Input;

using boda.net.wpf.core.Ioc;
using boda.net.wpf.share.Events;
using boda.net.wpf.share.Prism;

using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;

namespace boda.net.wpf.hmapplication.ViewModels
{
    public class MainViewModel : BindableBase
    {

        public ICommand LoadaCommand { get; set; }
        public string Title { get; set; } = "博达MES";
        public MainViewModel()
        {

            LoadaCommand = new DelegateCommand(LoadedCommandAction);
        }

        private void LoadedCommandAction()
        {
            IRegion mainRegion = PrismProvider.RegionManager.Regions[RegionNames.MainRegion];
            mainRegion.NavigationService.Navigated += NavigationService_Navigated;

            // 1. 加载Login模块
            PrismProvider.ModuleManager.LoadModule(ModuleNames.ApplicationLoginModule);
            // 2. 导航Login区域
            PrismProvider.RegionManager.RequestNavigate(RegionNames.MainRegion, ViewNames.LoginView);

            // 订阅用户登录成功事件
            PrismProvider.EventAggregator.GetEvent<LoginSuccessEvent>().Subscribe(m =>
            {
                // 登陆成功加载主界面模块，主界面模块会导航到主界面区域
                PrismProvider.ModuleManager.LoadModule(ModuleNames.ApplicationMainModule);

                PrismProvider.RegionManager.RequestNavigate(RegionNames.MainRegion, ViewNames.MainView);

            }, Prism.Events.ThreadOption.UIThread);
        }

        private Window _mainWindiw = Application.Current.MainWindow;
        /// <summary>
        /// 当主区域导航时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="System.NotImplementedException"></exception>
        private void NavigationService_Navigated(object? sender, RegionNavigationEventArgs e)
        {
            switch (e.Uri.OriginalString)
            {
                case ViewNames.LoginView:
                    _mainWindiw.Width = 800;
                    _mainWindiw.Height = 360;
                    //_mainWindiw.SizeToContent = SizeToContent.WidthAndHeight;
                    _mainWindiw.WindowStyle = WindowStyle.None;
                    _mainWindiw.WindowState = WindowState.Normal;
                    break;
                case ViewNames.MainView:
                    _mainWindiw.Width = 800;
                    _mainWindiw.Height = 360;
                    _mainWindiw.ResizeMode = ResizeMode.CanResize;
                    _mainWindiw.SizeToContent = SizeToContent.Manual;
                    _mainWindiw.WindowStyle = WindowStyle.None;
                    _mainWindiw.WindowState = WindowState.Normal;
                    break;
                default:
                    break;
            }
        }
    }
}
